/**
 * 20211215:抽离出检测函数getIntersects
 * 20220112:弃用
 */
let THREE=require('three');
import DragConfig from "./drag-config";
import Utils from "../general/utils";

/**
 * 网格体（mesh）拖动配置
 */
export default class DragMeshConfig extends DragConfig{
    constructor(values){
        console.log('this class is deprecated now!');
        super(values);
    }
    get type(){
        return 'dragMeshConfig';
    }
    checkType(object) {
        return object instanceof THREE.Mesh;
    }
    compare(a, b) {
        return a.uuid===b.uuid;
    }
    getIntersects(object,out=[]){
        return this._raycaster.intersectObject(object,false,out);
    }
    intersectObject(object,pointer,camera,rect) {
        Utils.setCaster(camera,rect,pointer,this._raycaster,pointer,this._pointerVector);
        return this.getIntersects(object,[]);
    }
    setDragStart(intersect,object,camera,rect){
        let camDir=this._tV0;
        camera.getWorldDirection(camDir);
        let objPos=this._tV1;
        object.getWorldPosition(objPos);
        if(intersect.point instanceof THREE.Vector3) {
            this._plane.setFromNormalAndCoplanarPoint(camDir, intersect.point);
        }else{
            this._plane.setFromNormalAndCoplanarPoint(camDir,objPos);
        }
        let intersection=this._tV0;
        if (this._raycaster.ray.intersectPlane(this._plane, intersection)) {
            this._offset.copy(intersection).sub(objPos);
        }
    }
    setDragMove(object,pointer,rect,camera,callback) {
        Utils.setCaster(camera,rect,pointer,this._raycaster,pointer,this._pointerVector);
        let intersection=this._tV0;
        if (this._raycaster.ray.intersectPlane(this._plane, intersection)) {
            this._wldPos.subVectors(intersection,this._offset);
            this._moveObject(object,this._wldPos,callback,camera);
        }
        if(!this._dynamicReferencePlane)return;
        let intersect = this.getIntersects(object,[]);
        if (intersect.length>0) {
            let _int = intersect[0];
            let camDir=this._tV0;
            camera.getWorldDirection(camDir);
            let objPos=this._tV1;
            object.getWorldPosition(objPos);
            if (_int.point instanceof THREE.Vector3) {
                this._plane.setFromNormalAndCoplanarPoint(camDir, _int.point);
            } else {
                this._plane.setFromNormalAndCoplanarPoint(camDir, objPos);
            }
        }
    }
    dispose() {
        super.dispose();
        this._pointerVector=null;
        this._raycaster=null;
        this._tV0=null;
        this._tV1=null;
        this._plane=null;
        this._offset=null;
        this._wldPos=null;
    }

    _moveObject(object, wldPos,callback,camera) {
        if(object.visible===false)return;
        let objPos=this._tV0;
        object.getWorldPosition(objPos);
        if (!this._checkRange(objPos))return;
        if(!this._limit)return;
        this._limit.modifyMove(object,wldPos,callback,camera);
    }

    /**
     * 检测拖动范围（球形区域）
     * @param v
     * @returns {boolean}
     * @private
     */
    _checkRange(v) {
        return v.lengthSq()<this._rangeSq;
    }


    _initVariable() {
        super._initVariable();
        this._pointerVector=new THREE.Vector2();
        this._raycaster=new THREE.Raycaster();
        this._tV0=new THREE.Vector3();
        this._tV1=new THREE.Vector3();
        this._plane=new THREE.Plane();
        this._offset=new THREE.Vector3();
        this._wldPos=new THREE.Vector3();
    }
}